<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>订单管理</title>
    <link rel="stylesheet" href="${ctxPath}/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="${ctxPath}/assets/module/admin.css">
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">泳池名称:</label>
                        <div class="layui-input-inline">
                            <input name="poolname" class="layui-input" placeholder="请输入泳池名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">教练名称:</label>
                        <div class="layui-input-inline">
                            <input name="coachname" class="layui-input" placeholder="请输入教练名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">订单状态:</label>
                        <div class="layui-input-inline">
                            <input name="state" class="layui-input" placeholder="请输入订单状态"/>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="tableSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary icon-btn">
                            <i class="layui-icon">&#xe669;</i>重置
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    {{# if(d.state=='待支付'){ }}
    {{# if(d.status==0){ }}
    <a class="layui-btn layui-btn-primary layui-btn-xs layui-border-green" perm-show="order:end" lay-event="end">完结</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs layui-border-green" perm-show="order:sp" lay-event="sp" >商品消费</a>
    {{# }  }}
    {{# if(d.status==1){ }}
    <a class="layui-btn layui-btn-primary layui-btn-xs layui-border-blue" perm-show="order:pay" lay-event="pay">支付</a>

    {{# }  }}
    <!--
    <a class="layui-btn layui-btn-primary layui-btn-xs" perm-show="order:edit" lay-event="edit">编辑</a>
-->
    <a class="layui-btn layui-btn-danger layui-btn-xs" perm-show="order:delete" lay-event="delete">删除</a>
    {{# } else if(d.state=='已支付'){   }}
    <a class="layui-btn layui-btn-primary layui-btn-xs layui-border-blue"  lay-event="pingfen">教练评分</a>
    <!--<a class="layui-btn layui-btn-primary layui-btn-xs" perm-show="order:edit" lay-event="edit">编辑</a>-->
    <a class="layui-btn layui-btn-danger layui-btn-xs" perm-show="order:delete" lay-event="delete">删除</a>
    {{# }else{   }}
<!--
    <a class="layui-btn layui-btn-primary layui-btn-xs" perm-show="order:edit" lay-event="edit">编辑</a>
-->
    <a class="layui-btn layui-btn-danger layui-btn-xs" perm-show="order:delete" lay-event="delete">删除</a>
    {{# }  }}
</script>

<!-- js部分 -->
<script src="${ctxPath}/assets/libs/layui/layui.js"></script>
<script src="${ctxPath}/assets/js/common.js"></script>
<script>
    layui.use(['layer', 'table', 'tableX', 'notice', 'xnUtil', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var table = layui.table;
        var tableX = layui.tableX;
        var admin = layui.admin;
        var form = layui.form;
        var notice = layui.notice;
        var xnUtil = layui.xnUtil;
        var laydate = layui.laydate;
        var vip = "您好";
        var insTb;
        console.log();
        $.ajaxSettings.async = false;
        $.get(getProjectUrl() +"getLoginUser",function(res){
            console.log(res);
            console.log(res.data.vip);
            vip = res.data.vip;
            if (res.data.roles[0].code=='yonghu'){
                insTb = tableX.render({
                    elem: '#dataTable',
                    url: getProjectUrl() + 'order/page',
                    where: {username:res.data.name},
                    page: true,
                    toolbar: ['<p>',
                        '<button lay-event="add" perm-show="order:add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                        '<button lay-event="delete" perm-show="order:delete" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                        '</p>'].join(''),
                    cellMinWidth: 100,
                    cols: [
                        [
                            {type: 'checkbox'},
                            {field: 'id', title: '订单号', sort: true,width:180},
                            {field: 'username', title: '用户名称', sort: true,width:110},
                            {field: 'poolname', title: '泳池名称', sort: true,width:110},
                            {field: 'coachname', title: '教练名称', sort: true,width:110},
                            {field: 'cost', title: '场地费/元', sort: true,width:110},
                            {field: 'coachprice', title: '教练费/元', sort: true,width:110},
                            {field: 'buy', title: '商品购买', sort: true,width:110},
                            {field: 'buyprice', title: '商品消费/元', sort: true,width:140},
                            {field: 'state', title: '订单状态', sort: true,width:110},
                            {field: 'status', hide: true},
                            {field: 'maketime', title: '预约时间', sort: true,width:120},

                            {field: 'paytime', title: '支付时间', sort: true,width:180},
                            {title: '操作', toolbar: '#tableBar', align: 'center', width: 200, minWidth: 200}
                        ]
                    ],
                    done: function(res, curr, count) {
                        xnUtil.tableDone(insTb, res, curr, count);
                    }
                });
            }
            else{
                insTb = tableX.render({
                    elem: '#dataTable',
                    url: getProjectUrl() + 'order/page',
                    page: true,
                    toolbar: ['<p>',
                        '<button lay-event="add" perm-show="order:add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                        '<button lay-event="delete" perm-show="order:delete" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                        '</p>'].join(''),
                    cellMinWidth: 100,
                    cols: [
                        [
                            {type: 'checkbox'},
                            {field: 'id', title: '订单号', sort: true,width:180},
                            {field: 'username', title: '用户名称', sort: true,width:110},
                            {field: 'poolname', title: '泳池名称', sort: true,width:110},
                            {field: 'coachname', title: '教练名称', sort: true,width:110},
                            {field: 'cost', title: '场地费/元', sort: true,width:110},
                            {field: 'coachprice', title: '教练费/元', sort: true,width:110},
                            {field: 'buy', title: '商品购买', sort: true,width:110},
                            {field: 'buyprice', title: '商品消费/元', sort: true,width:140},
                            {field: 'state', title: '订单状态', sort: true,width:110},
                            {field: 'status', hide: true},
                            {field: 'maketime', title: '预约时间', sort: true,width:120},

                            {field: 'paytime', title: '支付时间', sort: true,width:180},
                            {title: '操作', toolbar: '#tableBar', align: 'center', width: 200, minWidth: 200}
                        ]
                    ],
                    done: function(res, curr, count) {
                        xnUtil.tableDone(insTb, res, curr, count);
                    }
                });
            }
        })
        $.ajaxSettings.async = true;

        /* 渲染表格 */
      /*  var insTb = tableX.render({
            elem: '#dataTable',
            url: getProjectUrl() + 'order/page',
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" perm-show="order:add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="delete" perm-show="order:delete" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [
                [
                    {type: 'checkbox'},
                    {field: 'id', title: '订单号', sort: true,width:180},
                    {field: 'username', title: '用户名称', sort: true,width:110},
                    {field: 'poolname', title: '泳池名称', sort: true,width:110},
                    {field: 'coachname', title: '教练名称', sort: true,width:110},
                    {field: 'cost', title: '场地费/元', sort: true,width:110},
                    {field: 'coachprice', title: '教练费/元', sort: true,width:110},
                    {field: 'buy', title: '商品购买', sort: true,width:110},
                    {field: 'buyprice', title: '商品消费/元', sort: true,width:140},
                    {field: 'state', title: '订单状态', sort: true,width:110},
                    {field: 'status', hide: true},
                    {field: 'maketime', title: '预约时间', sort: true,width:120},

                    {field: 'paytime', title: '支付时间', sort: true,width:180},
                    {title: '操作', toolbar: '#tableBar', align: 'center', width: 200, minWidth: 200}
                ]
            ],
            done: function(res, curr, count) {
                xnUtil.tableDone(insTb, res, curr, count);
            }
        });*/

        /* 表格搜索 */
        form.on('submit(tableSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(dataTable)', function (obj) {
            /* 删除 */
            if (obj.event === 'delete') {
                layer.confirm('确定要操作该数据吗？', {
                    skin: 'layui-layer-admin',
                    shade: .1
                }, function () {
                    admin.req(getProjectUrl() + 'order/delete', JSON.stringify([{'id': obj.data.id}]), function(res){
                        layer.msg(res.message, {icon: 1, time: 1000}, function () {
                            insTb.reload();
                        });
                    }, 'post');
                });
            }
            /* 编辑 */
            if (obj.event === 'edit') {
                showAddOrUpdateModel(obj.data);
            }
            if (obj.event === 'pay') {
                pay(obj.data);
            }
            if (obj.event === 'pingfen') {
                pingfen(obj.data);
            }
            if (obj.event === 'sp') {
                sp(obj.data);
            }
            if (obj.event === 'end') {
                end(obj.data);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showAddOrUpdateModel();
            }
            if (obj.event === 'delete') { // 删除
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    notice.msg('请选择要操作的数据', {icon: 2});
                    return;
                }
                layer.confirm('确定要操作该数据吗？', {
                    skin: 'layui-layer-admin',
                    shade: .1
                }, function () {
                    var ids = checkRows.data.map(function (d) {
                        return {"id": d.id};
                    });
                    admin.req(getProjectUrl() + 'order/delete', JSON.stringify(ids), function(res){
                        layer.msg(res.message, {icon: 1, time: 1000}, function () {
                            insTb.reload();
                        });
                    }, 'post');
                });
            }
        });

        // 显示表单弹窗
        function showAddOrUpdateModel(data) {
            var layIndex = admin.open({
                title: (data ? '修改' : '添加') + '订单管理',
                url: getProjectUrl() + 'order/form',
                area: ['800px',''],
                data: { data: data },     // 传递数据到表单页面
                end: function () {
                    var layerData = admin.getLayerData(layIndex, 'formOk');
                    if (layerData) {  // 判断表单操作成功标识
                        insTb.reload();  // 成功刷新表格
                    }
                },
                success: function (layero, dIndex) {
                    // 弹窗超出范围不出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    $(layero).find('[lay-submit]').focus();
                }
            });
        }

        function pingfen(data) {
            var layIndex = admin.open({
                title: '评分',
                url: getProjectUrl() + 'order/pingfen',
                area: ['800px',''],
                data: { data: data },     // 传递数据到表单页面
                end: function () {
                    var layerData = admin.getLayerData(layIndex, 'formOk');
                    if (layerData) {  // 判断表单操作成功标识
                        insTb.reload();  // 成功刷新表格
                    }
                },
                success: function (layero, dIndex) {
                    // 弹窗超出范围不出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    $(layero).find('[lay-submit]').focus();
                }
            });
        }

        function sp(data) {
            window.location.href=getProjectUrl() + 'goods/index?id=' +data.id+ "&username=" + data.username;
        }
        function end(data) {
            layer.confirm('确定完结吗？完结后用户将可以支付此订单！', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function () {
                console.log(data);
                admin.req(getProjectUrl() + 'order/changeStatus', JSON.stringify(data), function(res){
                    if (res.code==-1){
                        layer.msg(res.message, {icon: 2, time: 1000}, function () {
                            insTb.reload();
                        });
                    }else{
                        layer.msg(res.message, {icon: 1, time: 1000}, function () {
                            insTb.reload();
                        });
                    }

                }, 'post');
            });        }
        var zhekou = 1;
        console.log(vip);
        console.log(vip=='gold');
        console.log(vip==='gold');
        if (vip=='gold'){
            vip = "您是尊贵的黄金会员";
            console.log(vip);
            zhekou = 0.9
        }
        if (vip=='platinum'){
            vip = "您是尊贵的铂金会员";
            console.log(vip);
            zhekou = 0.8
        }
        if (vip=='diamond'){
            vip = "您是尊贵的钻石会员";
            console.log(vip);
            zhekou = 0.7
        }

        function pay(data){
            layer.confirm(vip +',确定支付吗？\n(场地费：'+ data.cost +'元，教练费：'+ data.coachprice + '元，商品消费：' +data.buyprice+'元)'+'*'+zhekou+
                '\n总计：'+(Number(data.cost+data.coachprice+data.buyprice))*zhekou+"元", {
                skin: 'layui-layer-admin',
                shade: .1
            }, function () {
                console.log(data);
                data.cost= Number((data.cost+data.coachprice+data.buyprice)*zhekou);
                admin.req(getProjectUrl() + 'order/pay', JSON.stringify(data), function(res){
                    if (res.code==-1){
                        layer.msg(res.message, {icon: 2, time: 1000}, function () {
                            insTb.reload();
                        });
                    }else{
                        layer.msg(res.message, {icon: 1, time: 1000}, function () {
                            insTb.reload();
                        });
                    }

                }, 'post');
            });
        }
    });
</script>
</body>
</html>
